﻿using System;
using System.Collections.Generic;
using System.Linq;
using QuantoSharp.Core.CashFlows;

namespace QuantoSharp.Core.Calculator
{
    public class PresentValueCalculator:ICalculator
    {
        
        public PresentValueCalculator(IEnumerable<Cashflow> cashflows, double rate)
        {
            Cashflows = cashflows;
            Rate = rate;
        }

        public IEnumerable<Cashflow> Cashflows { get; private set; } 

        public double Rate { get; private set; }

        public double Calculate()
        {
            var pv = 0.0;

            var count = Cashflows.Count();

            for (int i = 0; i < count; i++)
            {
                pv += Cashflows.ElementAt(i).Amount / Math.Pow(1 + Rate, i);
            }

            pv = Math.Round(pv, 4);

            return pv;
        }
    }
}
